<?php
namespace app\api\controller;
use app\common\controller\Api;
use Think\Db;

class Request extends Api{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    // 根据 openID 生成订单数据
    function index(){  //微信支付

            $openid = $this->request->post('openid');
            $notify_url = "http://www.ymcnet.com/api/Notify/index";
            $fee = $this->request->post('fee');
            $rest = $this->request->post('order_on');
            require_once VENDOR_PATH . 'WxpayAPI/lib/WxPay.Api.php';
            //vendor('WxpayAPI.lib.WxPayApi');
            $total_fee = $fee*100;
//②、统一下单,生成预支付交易单prepayid
            $input = new \WxPayUnifiedOrder();
            $input->SetBody('购买通知');
//设置商品或支付单简要描述
            $input->SetAttach('');
            $input->SetOpenid($openid);
            $input->SetOut_trade_no($rest);
//设置商户系统内部的订单号,32个字符内、可包含字母, 其他说明见商户订单号
            $input->SetTotal_fee($total_fee);
//设置订单总金额，只能为整数，详见支付金额
            $input->SetTime_start(date("YmdHis"));
//设置订单生成时间，格式为yyyyMMddHHmmss
            $input->SetTime_expire(date("YmdHis", time() + 60*10));
//设置订单失效时间，格式为yyyyMMddHHmmss
            $input->SetNotify_url($notify_url);
//设置接收微信支付异步通知回调地址
            $input->SetTrade_type("JSAPI");
//设置类型如下：JSAPI，NATIVE，APP
            $order_data=\WxPayApi::unifiedOrder($input);
            //统一下单
            $order_data['timestamp'] = time();
//        dump($order_data);exit;
            $str = 'appid='.$order_data['appid'].'&noncestr='.$order_data['nonce_str'].'&package=Sign=WXPay&partnerid='.\WxPayConfig::MCHID.'&prepayid='.$order_data['prepay_id'].'&timestamp='.$order_data['timestamp'];
//③ 重新生成签名，并将结果返回给客户端
            $order_data['sign'] = strtoupper(md5($str.'&key='.\WxPayConfig::KEY));
            $parameter = array(
                'appid' => $order_data['appid'],
                'partnerid'=>$order_data['mch_id'],
                'prepayid'=>$order_data['prepay_id'],
                'package'=>'Sign=WXPay',
                'noncestr'=>$order_data['nonce_str'],
                'timestamp'=>$order_data['timestamp'],
                'sign' => $order_data['sign']
            );
            return json_encode($parameter);
        }



}